package com.lyf.alg.practice.nju.linetable;

/**
 * 求链表的平均值.
 *
 * @author liyunfei
 */
public class LinkAverage {
    static class ListNode{int data;ListNode next;}
    
    static ListNode mockData=new ListNode();
    
    static {
        mockData.data = 2;
        ListNode mockNextNode = new ListNode();
        mockNextNode.data=4;
        
        mockData.next = mockNextNode;
        
        ListNode mockNNextNode = new ListNode();
        mockNNextNode.data = 3;
        
        mockNextNode.next = mockNNextNode;
    }
    
    static double getAverageVal(ListNode list,int n){
        if(n==1){
            return list.data;
        }else{
            return (getAverageVal(list.next,n-1)*(n-1)+list.data)/n;
        }
    }
    // 类似-采用数组.
    
    public static void main(String[] args) {
        System.out.println(getAverageVal(mockData,3));
    }
}
